<template>
    <div class="page-container">
        <div class="grid-form-container">
            <div class="operation">
                <!-- 自己加点击事件 -->
                <el-button type="primary" >Operation1</el-button>
                <el-button type="primary" >Operation2</el-button>
            </div>
            <div class="search">
                <!-- 修改查询参数 -->
                <el-input v-model="query.param1" class="search-input-class" placeholder="参数1" />
                <el-input v-model="query.param2" class="search-input-class" placeholder="参数2" />
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="grid-container">
            <el-table ref="datagrid" :data="table.data" border @row-click="handleClickTableRow" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <!-- 修改列 -->
                <el-table-column prop="colum1" label="列1" />
                <el-table-column prop="colum2" label="列2" />
                <el-table-column prop="colum3" label="列3" />
                <el-table-column prop="colum4" label="列4" />
                <el-table-column prop="colum5" label="列5" />
            </el-table>
        </div>
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="table.pagination.currentPage" :page-sizes="table.pagination.pageSizes" :page-size="table.pagination.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="table.pagination.total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { ref, toRefs, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import http from '@/http.js'

export default {
    // 修改组件名称
    name: 'Demo',
    setup() {
        const router = useRouter();
        const route = useRoute();
        const datagrid = ref(null);

        const state = reactive({
            // 修改查询参数
            query: {
                param1: '',
                param2: ''
            },
            table: {
                data: [],
                selected: [],
                pagination: {
                    currentPage: 1,
                    pageSizes: [10, 20, 50, 100],
                    pageSize: 10,
                    total: 0
                }
            },
        });

        const handleSelectionChange = (val) => {
            state.table.selected = val;
        }

        const handleSizeChange = (val) => {
            state.table.pagination.pageSize = val;
            loadData();
        };

        const handleCurrentChange = (val) => {
            state.table.pagination.currentPage = val;
            loadData();
        };

        const handleClickTableRow = (row) => {
            datagrid.value.clearSelection();
            datagrid.value.toggleRowSelection(row);
        }

        const loadData = () => {
            // 修改查询参数
            let params = {
                param1: state.query.param1,
                param2: state.query.param2,
                page: state.table.pagination.currentPage,
                pageSize: state.table.pagination.pageSize
            }
            // 修改请求地址
            http.fetchForm({
                url: '/sys/user/queryGrid',
                data: params
            }).then((res) => {
                state.table.data = res.data.grid;
                state.table.pagination.total = res.data.total;
            });
        }

        const reset = () => {
            state.query.param1 = '';
            state.query.param2 = '';
            loadData();
        }

        onMounted(() => {
            loadData();
        });

        return {
            ...toRefs(state),
            datagrid,
            handleSelectionChange,
            handleSizeChange,
            handleCurrentChange,
            handleClickTableRow,
            loadData,
            reset
        };
    }
}
</script>
<style scoped>
.page-container {
    width: 100%;
    height: 100%;
}

.grid-form-container {
    width: 100%;
    padding: 20px 10px 10px 10px;
    display: flex;
    justify-content: space-between;
}

.search {
    display: flex;
}

.search-input-class {
    width: 180px;
    margin-right: 10px;
}

.grid-container {
    padding: 10px 10px 0 10px;
    height: calc(100% - 120px);
}

.pagination-container {
    padding: 10px;
}

.dialog-content {
    padding: 10px 20px;
}

.label-class {
    width: 100%;
    padding: 10px 0;
    font-size: 12px;
    color: #657180;
    font-weight: bold;
}

.content-class {
    width: 100%;
    padding: 0 0 10px 0;
}

.form-input-class {
    width: 100%;
}
</style>